#region OpenNETCF Copyright Information
/*
Copyright (c) 2007 OpenNETCF Consulting, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace InkNotesCF
{
    public abstract class NoteBase
    { 
        internal string m_inkData;
        internal string m_pageGuid;
        internal string m_objectGuid;
        internal string m_title;

        internal XmlDocument m_note;
        internal XmlNamespaceManager m_nsMgr;
        private string m_filename;

        public NoteBase()
        {
            LoadBlankNote();
        }

        internal abstract string XmlTemplate { get;}

        internal abstract string SchemaUrl { get; }

        internal abstract string NamespacePrefix { get;}

        public abstract string InkData { get; set;}

        public abstract string ObjectGuid { get; set;}

        public abstract string PageGuid { get; set;}

        public abstract string Title { get; set;}

        public bool IsNew
        {
            get
            {
                return m_filename == null;
            }
        }
      

        private void LoadBlankNote()
        {
            m_note = new XmlDocument();
            m_note.LoadXml(XmlTemplate);
            m_nsMgr = new XmlNamespaceManager(m_note.NameTable);
            if(SchemaUrl != null)
                m_nsMgr.AddNamespace(NamespacePrefix, SchemaUrl);
            m_filename = null;
            m_inkData = null;
            m_pageGuid = null;
            m_objectGuid = null;
            m_title = null;
        }

        public void LoadFromFile(string file)
        {
            LoadBlankNote();
            m_filename = file;
            StreamReader sr = new StreamReader(file);
            string data = sr.ReadToEnd();
            sr.Close();
            m_note.LoadXml(data);
        }

        public void Save()
        {
            m_note.Save(m_filename);
        }

        public void SaveToFile(string file)
        {
            m_filename = file;
            m_note.Save(file);
        }
    }

    public class Note2007 : NoteBase
    {
         internal override string XmlTemplate
        {
            get
            {
                return "<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/2007/onenote\" ID=\"{0}\">" +
                "<one:Title>" +
                    "<one:OE author=\"Mark Arteaga\" lastModifiedBy=\"Mark Arteaga\">"+
                        "<one:T>"+
/*                            "<![CDATA[]]>"+*/
                        "</one:T>"+
                    "</one:OE>"+
                "</one:Title>" +
                "<one:Outline>" +
                    "<one:OEChildren>" +
                        "<one:OE>"+
                            "<one:InkParagraph>"+
                                "<one:InkWord>"+
                                    "<one:Data>"+
                                    "</one:Data>"+
                                "</one:InkWord>"+
                             "</one:InkParagraph>"+
                        "</one:OE>"+
                    "</one:OEChildren>"+
                "</one:Outline>"+
            "</one:Page>"; ;
            }
        }

        internal override string SchemaUrl
        {
            get
            {
                return "http://schemas.microsoft.com/office/onenote/2007/onenote";
            }
        }

        internal override string NamespacePrefix
        {
            get { return "one"; }
        }

        private const string XPATH_DATA_NODE = "/one:Page/one:Outline/one:OEChildren/one:OE/one:InkParagraph/one:InkWord/one:Data";
        private const string XPATH_ENSUREPAGE_NODE = "/one:Page/one:Title/one:OE/one:T";

        public Note2007()
        {
        }

        /// <summary>
        /// Gets or sets the Ink Data in the note.  Ink Data is saved as ISF Base64
        /// </summary>
        public override string InkData
        {
            get
            {
                if (m_inkData == null)
                {
                    //Get from xmldocument
                    XmlNode node = m_note.SelectSingleNode(XPATH_DATA_NODE, m_nsMgr);
                    if (node != null)
                        m_inkData = "base64:" + node.InnerText + '\0'; //we need to add the base64 and \0 to the string to have it work on both the native and managed application
                }
                return m_inkData;
            }
            set
            {
                //Remove the base 64 adds base64 to the beginning of it for some reason.  This is to make it compatible with the native version as native does not add this
                if (value.StartsWith("base64:"))
                    value = value.Replace("base64:", "");
                if (value.EndsWith("\0"))
                    value = value.Substring(0, value.Length - 1);
                m_inkData = value;

                //Set it to the xml document
                XmlNode node = m_note.SelectSingleNode(XPATH_DATA_NODE, m_nsMgr);
                if (node != null)
                {
                    node.InnerText = m_inkData;
                   
                }
            }
        }

        public override string PageGuid
        {
            get
            {
                return ""; //Not needed for OneNote 2007.  The desktop applicaiton will handle it.
            }
            set
            {
            }
        }

        public override string ObjectGuid
        {
            get
            {
                return "";
            }
            set { }
        }

        public override string Title
        {
            get
            {
                if (m_title == null)
                {
                    //Get from xmldocument
                    XmlNode node = m_note.SelectSingleNode(XPATH_ENSUREPAGE_NODE, m_nsMgr);
                    if (node != null)
                    {
                        XmlAttribute att = node.Attributes["title"];
                        if (att != null)
                            m_title = att.Value;
                    }
                }
                return m_title;
            }
            set
            {
                m_title = value;
                XmlNode node = m_note.SelectSingleNode(XPATH_ENSUREPAGE_NODE, m_nsMgr);
                if (node != null)
                {
                    XmlNode data = m_note.CreateNode(XmlNodeType.CDATA, "", "");
                    data.InnerText = m_title;
                    node.AppendChild(data);
                }
            }
        }
    }
    /// <summary>
    /// Port of the PersistentNote in the InkNote sample for WISP in the Windows Mobile 6 SDK.  The template is compatible with OneNote 2003
    /// </summary>
    public class Note : NoteBase
    {

        internal override string XmlTemplate
        {
            get
            {
                return "<?xml version=\"1.0\" ?>" +
       "<Import xmlns=\"http://schemas.microsoft.com/office/onenote/2004/import\"> " +
         "<EnsurePage path=\"Pocket Notes\\InkFiles.one\" guid=\"\" title=\"\" /> " +
         "<PlaceObjects pagePath=\"Pocket Notes\\InkFiles.one\" pageGuid=\"\"> " +
               "<Object guid=\"\"> " +
                   "<Position x=\"0\" y=\"0\" />  " +
                   "<Ink> " +
                       "<Data></Data> " +
                   "</Ink> " +
               "</Object> " +
         "</PlaceObjects>" +
       "</Import>";
            }
        }

        internal override string SchemaUrl
        {
            get
            {
                return "http://schemas.microsoft.com/office/onenote/2004/import";
            }
        }

        internal override string NamespacePrefix
        {
            get { return "def"; }
        }

        private const string XPATH_DATA_NODE = "/def:Import/def:PlaceObjects/def:Object/def:Ink/def:Data";
        private const string XPATH_PAGE_NODE = "/def:Import/def:PlaceObjects";
        private const string XPATH_ENSUREPAGE_NODE = "/def:Import/def:EnsurePage";

        public Note()
        {
        }

        

        /// <summary>
        /// Gets or sets the Ink Data in the note.  Ink Data is saved as ISF Base64
        /// </summary>
        public override string InkData
        {
            get
            {
                if (m_inkData == null)
                {
                    //Get from xmldocument
                    XmlNode node = m_note.SelectSingleNode(XPATH_DATA_NODE, m_nsMgr);
                    if (node != null)
                        m_inkData = "base64:" + node.InnerText + '\0'; //we need to add the base64 and \0 to the string to have it work on both the native and managed application
                }
                return m_inkData;
            }
            set
            {
                //Remove the base 64 adds base64 to the beginning of it for some reason.  This is to make it compatible with the native version as native does not add this
                if (value.StartsWith("base64:"))
                    value = value.Replace("base64:", "");
                if (value.EndsWith("\0"))
                    value = value.Substring(0, value.Length - 1);
                m_inkData = value;

                //Set it to the xml document
                XmlNode node = m_note.SelectSingleNode(XPATH_DATA_NODE, m_nsMgr);
                if (node != null)
                    node.InnerText = m_inkData;
            }
        }

        public override string PageGuid
        {
            get
            {
                if (m_pageGuid == null)
                {
                    //Get from xmldocument
                    XmlNode node = m_note.SelectSingleNode(XPATH_PAGE_NODE, m_nsMgr);
                    if (node != null)
                    {
                        XmlAttribute att = node.Attributes["pageGuid"];
                        if (att != null)
                            m_pageGuid = att.Value;
                    }
                }
                return m_pageGuid;
            }
            set
            {
                m_pageGuid = value;
                //Get from xmldocument
                XmlNode node = m_note.SelectSingleNode(XPATH_PAGE_NODE, m_nsMgr);
                if (node != null)
                {
                    XmlAttribute att = node.Attributes["pageGuid"];
                    if (att != null)
                        att.Value = m_pageGuid;
                }
            }
        }

        public override string ObjectGuid
        {
            get
            {
                if (m_objectGuid == null)
                {
                    //Get from xmldocument
                    XmlNode node = m_note.SelectSingleNode(XPATH_ENSUREPAGE_NODE, m_nsMgr);
                    if (node != null)
                    {
                        XmlAttribute att = node.Attributes["guid"];
                        if (att != null)
                            m_objectGuid = att.Value;
                    }
                }
                return m_objectGuid;
            }
            set
            {
                m_objectGuid = value;
                XmlNode node = m_note.SelectSingleNode(XPATH_ENSUREPAGE_NODE, m_nsMgr);
                if (node != null)
                {
                    XmlAttribute att = node.Attributes["guid"];
                    if (att != null)
                        att.Value = m_objectGuid;
                }
            }
        }

        public override string Title
        {
            get
            {
                if (m_title == null)
                {
                    //Get from xmldocument
                    XmlNode node = m_note.SelectSingleNode(XPATH_ENSUREPAGE_NODE, m_nsMgr);
                    if (node != null)
                    {
                        XmlAttribute att = node.Attributes["title"];
                        if (att != null)
                            m_title = att.Value;
                    }
                }
                return m_title;
            }
            set
            {
                m_title = value;
                XmlNode node = m_note.SelectSingleNode(XPATH_ENSUREPAGE_NODE, m_nsMgr);
                if (node != null)
                {
                    XmlAttribute att = node.Attributes["title"];
                    if (att != null)
                        att.Value = m_title;
                }
            }
        }

    }
}
